<?php
$dealer_id = $this->getRequest()->getParam('dealer_id');
$manufacturer_id = $this->getRequest()->getParam('manufacturer_id');
$product_name = $this->getRequest()->getParam('product_name');
$orderIds = $this->getRequest()->getPost('order_ids');
$_coreHelper = $this->helper('core');
$startDate = $this->getRequest()->getParam('start_date','');
$endDate = $this->getRequest()->getParam('end_date','');
$province_id = $this->getRequest()->getParam('province_id')?$this->getRequest()->getParam('province_id'):533;
$city_id = $this->getRequest()->getParam('city_id')?$this->getRequest()->getParam('city_id'):160;
$website_id = $this->getRequest()->getParam('website_id');

$session = Mage::getSingleton('admin/session');
$roleId = $session->getUser()->getRoleId();
$roles = Mage::helper('zgb')->getUserRoles();
$zgb = Mage::helper('zgb');

$items = $this->getItemCollection();
?>
<div class="content-header">
    <table cellspacing="0">
        <tbody>
        	<tr>
	            <td style="width:50%;"><h3 class="icon-head head-sales-order">销售统计报表</h3></td>
	            <td class="form-buttons"></td>
        	</tr>
    	</tbody>
	</table>
</div>

<div class="entry-edit">
	<div class="report-header">
		<div class="search-box fieldset">
			<form name="report_form" id="report_form" action="" method="get">
				<table class="form-list">
					<tr>
						<td class="label"><label>统计日期:</label></td>
						<td class="value">
							<input type="text" name="start_date" id="start_date" value="<?php echo $startDate;?>" class="laydate-icon"/> 至 <input type="text" name="end_date" id="end_date" value="<?php echo $endDate;?>" class="laydate-icon"/>
						</td>
					</tr>
					<tr>
						<td class="label">商品名称:</td>
						<td class="value">
							<input type="text" name="product_name" id="product_name" value="<?php echo $product_name;?>"/>
						</td>
					</tr>
					<?php if(!$session->getUser()->getWebsiteId()):?>
					<tr>
						<?php $websites = Mage::getModel('core/website')->getCollection();?>
						<td class="label">站点:</td>
						<td class="value">
							<select id="r_website_id" name="website_id">
								<option value="">--请选择--</option>
								<?php foreach($websites as $website):?>
									<?php if($website->getId()==1) continue;?>
									<option value="<?php echo $website->getId();?>" <?php if($website->getId()==$website_id) echo 'selected';?>><?php echo $website->getName();?></option>
								<?php endforeach;?>
							</select>
						</td>
					</tr>
					<?php endif;?>
					<tr>
						<td colspan="2" style="text-align:right">
							<input type="submit" value="统计" name="apply" id="apply"/> <input type="reset" value="重置" id="reset"/> <input type="submit" value="导出" name="export" id="export"/>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		laydate({
		    elem: '#start_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
		laydate({
		    elem: '#end_date',
		    format: 'YYYY-MM-DD',
		    event: 'focus'
		});
	</script>
	<div class="grid">
		<table class="data">
			<thead>
				<tr class="headings">
					<td width="8%">区域</td>
					<td>商品名称</td>
					<td width="8%">单位</td>
					<td width="8%">数量</td>
					<td width="8%">单价</td>
					<td width="8%">合计金额</td>
				</tr>
			</thead>
			<tbody>
			<?php foreach($items as $item):?>
				<?php if(!$item['name']) continue;?>
				<tr>
					<td><?php echo $item['website_name'] ?></td>
					<td><?php echo $item['name']; ?></td>
					<td><?php echo $item['unit']; ?></td>
					<td><?php echo intval($item['qty']); ?></td>
					<td><?php echo $_coreHelper->formatPrice($item['price'],false); ?></td>
					<td><?php echo $_coreHelper->formatPrice($item['row_total'],false); ?></td>
				</tr>
			<?php endforeach;?>
			</tbody>
		</table>
	</div>
</div>
<script type="text/javascript">
(function($){
	// $("#apply").click(function(){
	// 	if($("#r_province_id").val())
	// 	{
	// 		if(!$("#r_city_id").val())
	// 		{
	// 			alert("请选择城市");
	// 			return;
	// 		}
	// 	}
	// 	$("#report_form").submit();
	// });
	// if($('#r_province_id').val()=='') 
 //    {
 //        $('#r_city_id').attr('disabled',true);
 //        $('#r_website_id').attr('disabled',true);
 //    }
	$('#r_province_id').change(function(){      
        $.getJSON('<?php echo $this->getUrl('zgb/ajax/citys')?>?id='+$(this).val(), function(data){
            $('#r_city_id').empty();
            $('#r_website_id').empty();
            var option = $('<option>').val('').text('-请选择-');
            $('#r_website_id').append(option);
            for(var i=0; i<data.length;i++)
            {
                var option = $('<option>').val(data[i].value).text(data[i].label);
                $('#r_city_id').append(option);
            }
            $('#r_city_id').attr('disabled',false);
            $('#r_website_id').attr('disabled',true);
            $('#r_city_id').find('option:first').attr('selected',true);
        });
    });

    $("#r_city_id").change(function(){
        $('#r_website_id').attr('disabled',true);
        $.getJSON('<?php echo $this->getUrl('zgb/ajax/websites')?>?id='+$(this).val(), function(data){
            $('#r_website_id').empty();
            for(var i=0; i<data.length;i++)
            {
                var option = $('<option>').val(data[i].value).text(data[i].label);
                $('#r_website_id').append(option);
            }
            $('#r_website_id').attr('disabled',false);
            $('#r_website_id').find('option:first').attr('selected',true);
        });
    });


})(jQuery);
</script>